package com.whub507.msgmanager.controller;

import com.whub507.msgmanager.common.Field;
import com.whub507.msgmanager.common.ResResult;
import com.whub507.msgmanager.domain.Message;
import com.whub507.msgmanager.service.MessageService;
import com.whub507.msgmanager.task.ScheduledTaskManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@Api(tags = "短信管理接口")
@RequestMapping("/msg")
public class MessageController {

    @Resource
    MessageService messageService;

    @Resource
    ScheduledTaskManager scheduledTaskManager;

    @GetMapping("/templates")
    @ApiOperation(value = "下载短信模板")
    public ResResult<Boolean> downloadMsgTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        messageService.downloadMsgTemplate(request,response);
        return ResResult.ok(Boolean.TRUE,"短信发送任务模板下载成功");
    }

    @PostMapping("/upload")
    @ApiOperation(value = "上传填写好的短信信息(状态：未提交)")
    public ResResult<Boolean> uploadMsg(MultipartFile file,Integer projectId){
        return messageService.uploadMessage(file,projectId);
    }

    @PostMapping("/formUpload")
    @ApiOperation(value = "表单上传填写好的短信信息（状态：未提交）")
    public ResResult<Boolean> formUploadMsg(Message msg){
        return messageService.formUploadMessage(msg);
    }

    @PostMapping("/validate")
    @ApiOperation(value = "确认短信无误并加入定时队列或立即发送")
    public ResResult<Boolean> validateMsg(Integer projectId, @RequestParam("nodeAndTitles") List<String> nodeAndTitles) throws ParseException {
        return messageService.validateMessage(projectId, nodeAndTitles);
    }

    @PostMapping("/edit")
    @ApiOperation(value = "编辑短信")
    public ResResult<Boolean> editMsg(Message message, String preNode, String preTitle){
        return messageService.editMsg(message, preNode, preTitle);
    }

    @GetMapping("/search")
    @ApiOperation(value = "查询短信")
    public ResResult<List<Message>> searchMessage(Integer projectId, String keyWord){
        List<Field> fields=new ArrayList<>();
        fields.add(new Field("projectId","项目id"));
        fields.add(new Field("title","短信标题"));
        fields.add(new Field("type","短信类型"));
        fields.add(new Field("senttime","发送时间"));
        fields.add(new Field("phonenums","发送对象"));
        fields.add(new Field("node","项目节点"));
        fields.add(new Field("content","短信内容"));
        fields.add(new Field("done","是否已发送"));
        List<Message> messageList = messageService.searchMessage(projectId, keyWord);
        return ResResult.ok(messageList, fields, "查询短信返回结果");
    }

    @GetMapping("/all")
    @ApiOperation(value = "获取所有短信")
    public ResResult<List<Message>> getAllMsg(Integer projectId){
        List<Field> fields=new ArrayList<>();
        fields.add(new Field("projectId","项目id"));
        fields.add(new Field("title","短信标题"));
        fields.add(new Field("type","短信类型"));
        fields.add(new Field("senttime","发送时间"));
        fields.add(new Field("phonenums","发送对象"));
        fields.add(new Field("node","项目节点"));
        fields.add(new Field("content","短信内容"));
        fields.add(new Field("done","是否已发送"));
        List<Message> messageList=messageService.getAllMsgs(projectId);
        return ResResult.ok(messageList,fields,"项目下短信任务已全部获取");
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除短信")
    public ResResult<Boolean> deleteMsg(Integer projectId, @RequestParam("nodeAndTitles") List<String> nodeAndTitles){
        return ResResult.ok(messageService.deleteMsg(projectId, nodeAndTitles),"该短信发送任务已删除");
    }

    @GetMapping("/download")
    @ApiOperation(value = "下载短信模板")
    public ResResult<Boolean> downloladMsg(HttpServletRequest request, HttpServletResponse response, Integer projectId,String node, String title) throws IOException {
        return ResResult.ok(messageService.downloadMsg(request,response,projectId,node,title),"短信发送任务信息下载成功");
    }


    @GetMapping("/test")
    public void sentTest(String time) throws ParseException {
        scheduledTaskManager.add("测试发送",time);
    }
}
